package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

public abstract class BaseStudentController {
    Scanner sc = new Scanner(System.in);
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseStudentController.class);

    //主菜单
    public final void studentStart() {
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢使用学生管理系统,再见");
                    return;
                default:
                    System.out.println("输入有误,请重新输入:");
            }
        }
    }

    private Socket getSocket() throws IOException {
        return new Socket("127.0.0.1", 10088);
    }

    //修改学生
    public final void updateStudent() {
        try {
            Socket socket = getSocket();
            ArrayList<Student> list = findAllStudent();
            if (list.isEmpty()) {
                System.out.println("系统暂无数据,请先添加数据:");
                return;
            }

            String updateId = inputStudentId("true");
            Student newStu = getStudent(updateId);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[3]," + newStu.toString());
            bw.flush();
            socket.shutdownOutput();

            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String s = br.readLine();
            LOGGER.debug("修改学生信息回显的数据"+s);
            System.out.println(s);
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //删除学生
    public final void deleteStudent() {
        try {
            Socket socket = getSocket();
            ArrayList<Student> list = findAllStudent();
            if (list.isEmpty()) {
                System.out.println("系统暂无数据,请先添加数据");
                return;
            }
            String delId = inputStudentId("true");
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[2]," + delId);
            bw.flush();
            socket.shutdownOutput();

            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String s = br.readLine();
            LOGGER.debug("删除学生回显的数据"+s);
            System.out.println(s);
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //添加学生
    public final void addStudent() {
        String sid = null;
        try {
            Socket socket = getSocket();
            sid = inputStudentId("false");
            Student student = getStudent(sid);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[1]," + student.toString());
            bw.flush();
            socket.shutdownOutput();

            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String s = br.readLine();
            socket.close();
            if (s.equals("true")) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取数组仓库,判断数组仓库是否为空
    public final ArrayList<Student> findAllStudent() {
        //发送tcp协议到服务器,将服务器的数据拿出来
        ArrayList<Student> list = null;
        try {
            Socket socket = getSocket();
            //给服务器一个指令
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[4]");
            bw.flush();
            bw.newLine();
            socket.shutdownOutput();

            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            list = (ArrayList<Student>) ois.readObject();
            System.out.println("----------------------------------------");
            //展示学生信息
            System.out.println("学号\t\t" + "姓名\t" + "年龄\t" + "生日");
            System.out.println("----------------------------------------");
            for (int i = 0; i < list.size(); i++) {
                Student stu = list.get(i);
                if (stu != null) {
                    System.out.println(stu.getSid() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
                }
            }
            System.out.println("----------------------------------------");
            //释放资源
            socket.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    //判断学号是否存在
    public final String inputStudentId(String b) throws IOException {
        System.out.println("请输入学号:");
        String id;
        while (true) {
            //获取用户录入的学生好号
            id = sc.next();
            Socket socket = getSocket();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[5]," + id);
            bw.flush();
            socket.shutdownOutput();

            //获取服务器端返回的数据
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String res = br.readLine();

            if (b.equals(res)) {
                break;
            } else {
                System.out.println("学号存在,请重新输入:");
            }
        }
        return id;
    }

    //用户输入创建学生对象
    public abstract Student getStudent(String sid);
}
